package dao

import (
	"errors"
	"order_srv/basic/config"
	__ "order_srv/basic/orderproto"
	"order_srv/handler/models"
)

// CreateRating 统一创建评价的函数
// 同时支持司机评价乘客和乘客评价司机
// 通过type字段区分评价方向
func CreateRating(in *__.RatingReq) (int64, error) {
	// 1. 验证订单是否存在且已完成
	var order models.Orders
	err := order.GetOrderInfo(int(in.OrderId))
	if err != nil {
		return 0, errors.New("订单不存在" + err.Error())
	}
	if order.OrderStatus != 5 {
		return 0, errors.New("订单未完成，无法评价")
	}

	// 2. 验证评价者与订单是否匹配
	ratingType := models.RatingType(in.RatingType)
	raterId := in.RaterId
	targetId := in.TargetId

	// 根据评价类型验证评价者与订单的匹配关系
	switch ratingType {
	case models.RatingTypePassengerToDriver:
		// 乘客评价司机，验证用户ID是否匹配订单中的用户ID
		if order.UserId != raterId {
			return 0, errors.New("乘客与订单不匹配")
		}
		// 确保目标ID是订单中的司机ID
		targetId = order.DriverId
	case models.RatingTypeDriverToPassenger:
		// 司机评价乘客，验证司机ID是否匹配订单中的司机ID
		if order.DriverId != raterId {
			return 0, errors.New("司机与订单不匹配")
		}
		// 确保目标ID是订单中的用户ID
		targetId = order.UserId
	default:
		return 0, errors.New("无效的评价类型")
	}

	// 3. 验证是否已经评价过
	var existingRating models.Rating
	err = existingRating.GetRatingByOrderAndRater(in.OrderId, raterId, ratingType)
	if err == nil && existingRating.Id > 0 {
		return 0, errors.New("已经评价过")
	}

	// 4. 创建统一评价
	var r models.Rating
	r.OrderId = in.OrderId
	r.RaterId = raterId
	r.TargetId = targetId
	r.RatingType = int(in.RatingType)
	r.Score = uint8(in.Score)
	r.Comment = in.Comment
	r.Tags = in.Tags
	r.Images = in.Images

	if err := r.CreateRating(); err != nil {
		return 0, errors.New("创建评价失败" + err.Error())
	}

	return int64(r.Id), nil
}

// GetRatingsByTargetId 获取用户收到的所有评价
func GetRatingsByTargetId(targetId uint32, ratingType int) ([]*__.RatingInfo, error) {
	ratings, err := models.GetRatingsByTargetId(targetId, models.RatingType(ratingType))
	if err != nil {
		return nil, errors.New("查询评价失败" + err.Error())
	}

	// 转换为protobuf格式
	var ratingInfos []*__.RatingInfo
	for _, r := range ratings {
		info := &__.RatingInfo{
			Id:         int64(r.Id),
			OrderId:    r.OrderId,
			RaterId:    r.RaterId,
			TargetId:   r.TargetId,
			RatingType: int32(r.RatingType),
			Score:      int32(r.Score),
			Comment:    r.Comment,
			Tags:       r.Tags,
			Images:     r.Images,
			CreateAt:   r.CreateAt.Format("2006-01-02 15:04:05"),
		}
		ratingInfos = append(ratingInfos, info)
	}

	return ratingInfos, nil
}

// GetAverageRating 获取用户平均评分
func GetAverageRating(targetId uint32, ratingType int) (float32, error) {
	avgRating, err := models.GetAverageRating(targetId, models.RatingType(ratingType))
	if err != nil {
		return 0, errors.New("计算平均评分失败" + err.Error())
	}
	return float32(avgRating), nil
}

// GetRatingByOrder 获取指定订单的评价
func GetRatingByOrder(orderId uint32, ratingType int) (*__.RatingInfo, error) {
	var r models.Rating
	err := config.DB.Where("order_id = ? AND rating_type = ?", orderId, ratingType).First(&r).Error
	if err != nil {
		return nil, errors.New("查询评价失败" + err.Error())
	}

	return &__.RatingInfo{
		Id:         int64(r.Id),
		OrderId:    r.OrderId,
		RaterId:    r.RaterId,
		TargetId:   r.TargetId,
		RatingType: int32(r.RatingType),
		Score:      int32(r.Score),
		Comment:    r.Comment,
		Tags:       r.Tags,
		Images:     r.Images,
		CreateAt:   r.CreateAt.Format("2006-01-02 15:04:05"),
	}, nil
}
